<?php
namespace Home\Controller;
use Think\Controller;

header("content-Type: text/html; charset=Utf-8");

class XmlController extends Controller {



	public function newXml(){
		$app = "app";
		$kiosk = "kiosk";
		$this->xml("en", $app);
		$this->xml("sc", $app);
		$this->xml("tc", $app);

		$this->xml("en", $kiosk);
		$this->xml("sc", $kiosk);
		$this->xml("tc", $kiosk);
	}

	public function xml($lang, $device){

		$menu_name = "menu_name_".$lang;
		$link_name = "link_name_".$lang;
		$link_addr = "link_addr_".$lang;
		$filename = "Config_".$device."_".$lang.".xml";

		switch ($lang) {
			case 'en':
			$xml = "<?xml version='1.0' encoding='utf-8'?>
	<CyberMedia>
		<Fonts>
			<Font Path='Fonts/Avenir.swf'/>
		</Fonts>
		<Style Path='style.css'/>
		<Configs>
			<Config Name='Language' Value='en'/>
			<Config Name='IdleTime' Value='180'/>
			<Config Name='TransitionSpeed' Value='1920'/>
			<Config Name='ScreenSaver' Value='Particle Transition(1920x1080)-5_2.wmv'/>
			<Config Name='hasCamera' Value='no'/>
		</Configs>
		<Menus>\r\n";				
				break;

			case 'sc':
			$xml = "<?xml version='1.0' encoding='utf-8'?>
	<CyberMedia>
		<Fonts>
			<Font Path='Fonts/Microsoft JhengHei.swf'/>
		</Fonts>
		<Style Path='style.css'/>
		<Configs>
			<Config Name='Language' Value='cn'/>
			<Config Name='IdleTime' Value='180'/>
			<Config Name='TransitionSpeed' Value='1920'/>
			<Config Name='ScreenSaver' Value='Particle Transition(1920x1080)-5_2.wmv'/>
			<Config Name='hasCamera' Value='no'/>
		</Configs>
		<Menus>\r\n";				
				break;				

			case 'tc':
			$xml = "<?xml version='1.0' encoding='utf-8'?>
	<CyberMedia>
		<Fonts>
			<Font Path='Fonts/Microsoft JhengHei.swf'/>
		</Fonts>
		<Style Path='style.css'/>
		<Configs>
			<Config Name='Language' Value='zh'/>
			<Config Name='IdleTime' Value='180'/>
			<Config Name='TransitionSpeed' Value='1920'/>
			<Config Name='ScreenSaver' Value='Particle Transition(1920x1080)-5_2.wmv'/>
			<Config Name='hasCamera' Value='no'/>
		</Configs>
		<Menus>\r\n";				
				break;			
			default:
				# code...
				break;
		}


		$db_menu = M('menu');
		$db_link = M('link');

		if($device=='app'){
			$condition['app'] =1;
		}
		$condition['level']=1;	

		$menu_first_level = $db_menu->where($condition)->order('menu_name_en asc')->select();											//first level menu

		// first level start:
		for($i=0; $i<count($menu_first_level); $i++){
			$xml = $xml."		<Menu Name="."\"".$menu_first_level[$i][$menu_name]."\"".">"."\r\n";				

			// second level start:
			$menu_second_level = $db_menu->where("parent_id='%d'", $menu_first_level[$i]["menu_id"])->order('menu_name_en asc')->select();		//second level menu
			for($j=0;$j<count($menu_second_level);$j++){
				$xml = $xml."			<Menu Name="."\"".$menu_second_level[$j][$menu_name]."\""." FontSize=\"".$menu_second_level[$j]["font_size"]."\" ".">"."\r\n";	
				
				//third level start:

				$menu_third_level = $db_menu->where("parent_id='%d'", $menu_second_level[$j]["menu_id"])->order('menu_name_en asc')->select();	//third level menu
				for($k=0; $k<count($menu_third_level); $k++){
					$xml = $xml."				<Menu Name="."\"".$menu_third_level[$k][$menu_name]."\""." FontSize=\"".$menu_third_level[$k]["font_size"]."\" ".">"."\r\n";

					//forth level start:
					$link = $db_link->where("parent_id='%d'", $menu_third_level[$k]["menu_id"])->order('link_name_en asc')->select();		
					for($t=0; $t<count($link);$t++){
						$xml = $xml."					<Menu Name="."\"".$link[$t][$link_name]."\""." FontSize=\"".$link[$t]["font_size"]."\" "." URL="."\"".$link[$t][$link_addr]."\""."/>"."\r\n";
					}
					//forth level end. 
					$xml = $xml."					</Menu>"."\r\n";
				}

				$link = $db_link->where("parent_id='%d'", $menu_second_level[$j]["menu_id"])->order('link_name_en asc')->select();		//third level link
				for($k=0; $k<count($link);$k++){
					$xml = $xml."				<Menu Name="."\"".$link[$k][$link_name]."\""." FontSize=\"".$link[$k]["font_size"]."\" "." URL="."\"".$link[$k][$link_addr]."\""."/>"."\r\n";
				}
				// third level end.

				$xml = $xml."				</Menu>"."\r\n";																	
			}
			

			$link = $db_link->where("parent_id='%d'", $menu_first_level[$i]['menu_id'])->order('link_name_en asc')->select();			//second level link
			for($j=0; $j<count($link); $j++){
				$xml = $xml."			<Menu Name="."\"".$link[$j][$link_name]."\""." FontSize=\"".$link[$j]["font_size"]."\" "." URL="."\"".$link[$j][$link_addr]."\""."/>"."\r\n";
			}

			// second level end.
			$xml = $xml."		</Menu>"."\r\n";
		}

		$link = $db_link->where("parent_id='%d'", 0)->order('link_name_en asc')->select();												//first level link
		for($i=0; $i<count($link); $i++){
			$xml = $xml."		<Menu Name="."\"".$link[$i][$link_name]."\""." FontSize=\"".$link[$i]["font_size"]."\" "." URL="."\"".$link[$i][$link_addr]."\""."/>"."\r\n";
		}
		// first level end.

/*		if($device=="kiosk"){
			switch ($lang) {
				case 'en':
					$xml = $xml."
			<Menu Name='location & neighbourhood' URL='http://www.lodgewood.com.hk/en/wanchai/' />
			<Menu Name='news & promotions'>
				<Menu Name='Apartment news' URL='http://www.lodgewood.com.hk/en/wanchai/' FontSize= '19'/>
			</Menu >
			<Menu Name='services directory' URL='' />
			<Menu Name='join Vivva membership' URL='http://member.lhotelgroup.com' />
			<Menu Name='News Center'>
				<Menu Name='Metro Daily' FontSize='19' URL='http://www.metrohk.com.hk/'/>
				<Menu Name='Apple Daily' FontSize='19' URL='http://hk.apple.nextmedia.com'/>
				<Menu Name='am730' FontSize='19' URL='http://www.am730.com.hk/home'/>
				<Menu Name='Oriental Daily' FontSize='19' URL='http://orientaldaily.on.cc/'/>
				<Menu Name='South China Morning Post' FontSize='19' URL='http://www.scmp.com/frontpage/hk'/>
				<Menu Name='The Standard' FontSize='19' URL='http://thestandard.com.hk/'/>
			</Menu>";
					break;
				case 'sc':
					$xml = $xml."
			<Menu Name='位置及邻近社区' URL='http://www.lodgewood.com.hk/sc/wanchai/' />
			<Menu Name='最新讯息和推广'>
				<Menu Name='住宅最新讯息' URL='http://www.lodgewood.com.hk/sc/wanchai/' FontSize= '19'/>
			</Menu >			
			<Menu Name='服务指南' URL='' />
			<Menu Name='成为Vivva会员' URL='http://member.lhotelgroup.com/Portal/zh/main.aspx' />
			<Menu Name='新闻资讯'>
				<Menu Name='都市日报' FontSize='19' URL='http://www.metrohk.com.hk/'/>
				<Menu Name='苹果日报' FontSize='19' URL='http://hk.apple.nextmedia.com'/>
				<Menu Name='am730' FontSize='19' URL='http://www.am730.com.hk/home'/>
				<Menu Name='东方日报' FontSize='19' URL='http://orientaldaily.on.cc/'/>
				<Menu Name='南华早报' FontSize='19' URL='http://www.scmp.com/frontpage/hk'/>
				<Menu Name='英文虎报' FontSize='19' URL='http://thestandard.com.hk/'/>
			</Menu>";
					break;
				case 'tc':
					$xml = $xml."
			<Menu Name='位置及鄰近社區' URL='http://www.lodgewood.com.hk/tc/wanchai/' />
			<Menu Name='最新訊息和推廣'>
				<Menu Name='住宅最新讯息' URL='http://www.lodgewood.com.hk/tc/wanchai/' FontSize= '19'/>
			</Menu >
			<Menu Name='服務指南' URL='' />
			<Menu Name='成為Vivva會員' URL='http://member.lhotelgroup.com/Portal/hk/main.aspx' />
			<Menu Name='新聞資訊'>
				<Menu Name='都市日報' FontSize='19' URL='http://www.metrohk.com.hk/'/>
				<Menu Name='蘋果日報' FontSize='19' URL='http://hk.apple.nextmedia.com'/>
				<Menu Name='am730' FontSize='19' URL='http://www.am730.com.hk/home'/>
				<Menu Name='東方日報' FontSize='19' URL='http://orientaldaily.on.cc/'/>
				<Menu Name='南華早報' FontSize='19' URL='http://www.scmp.com/frontpage/hk'/>
				<Menu Name='英文虎報' FontSize='19' URL='http://thestandard.com.hk/'/>
			</Menu>";
					break;						
				default:
					# code...
					break;
			}
		}*/

		$xml = $xml."	</Menus>

	</CyberMedia>";

		//$xml = str_replace("&", "&amp;", $xml);
		file_put_contents($filename, $xml);

		// update xml finished


		// update xml version:
		$db_xml = M('xml');
		$db_xml->where("xml_id='%d'", 1)->setInc('version', 1);

	}


    
	// load Config_en.xml into database for table link
    public function load_xml(){
    	$db_link=M('link');
    	$db_link->where('1')->delete();
    	$db_menu = M('menu');
    	$db_menu->where('1')->delete();

		$dom=new \DOMDocument();
		$file = "http://localhost/Public/xml/weblink_en.xml";
		$res = $dom->load($file);
		if(!$res){
			die("error: open failed");
		}

		$links=$dom->getElementsByTagName(Menu);

		$parent = "";
		foreach ($links as $link) {
			$name = $link->getAttribute("Name");
			$url = $link->getAttribute("URL");
			$font_size = $link->getAttribute("FontSize");
			if($parent==""){
				$parent=$name;
			}
			if($url!=""){
				echo "<p>".$name." : ".$url."/".$font_size."</p>";

				$link_data['link_name_en']=$name;
				$link_data['link_addr_en']=$url;
				$link_data['parent_name']=$parent;
				$link_data['font_size']=$font_size;
				$db_link->add($link_data);
			}else{
				$parent=$name;
				$menu_data['menu_name_en']=$name;
				$menu_data['font_size']=$font_size;
				$db_menu->add($menu_data);
			}
		}

		// match link with menu in table link

		$res = $db_link->select();
		for($i=0; $i<count($res); $i++){
			//echo $res[$i]['parent'];
			$parent = $db_menu->where("menu_name_en='%s'", $res[$i]['parent_name'])->select();
			if($parent){
				$data['parent_id']= $parent[0]['menu_id'];
				$ret = $db_link->where("link_id= '%d'", $res[$i]['link_id'])->save($data);
			}
		}

		// set menu level
		$res = $db_menu->select();
		for($i=0; $i<count($res); $i++){

			$find = $db_link->where("parent_name= '%s'", $res[$i]['menu_name_en'])->find();
			if($find){
				$data['level']=3;
				$db_menu->where("menu_id = '%d'", $res[$i]['menu_id'])->save($data);
				echo $i;
			}

		}
	}

	// load Config_tc.xml into database
    public function link_sc(){
    	$db_link=M('link');
    	$db_menu = M('menu');
		$dom=new \DOMDocument();
		$file = "http://localhost/Public/xml/weblink_sc.xml";
		$res = $dom->load($file);
		$links=$dom->getElementsByTagName(Menu);

		$link_id = 1;
		$menu_id = 1;
		foreach ($links as $link) {
			$name = $link->getAttribute("Name");
			$url = $link->getAttribute("URL");
			if($url!=""){
				$data['link_name_sc']=$name;
				$data['link_addr_sc']=$url;
				echo $link_id."  ".$name." ".$url."<br/>";
				$db_link->where("link_id='%d'", $link_id)->save($data);
				$link_id++;
			}else{
				echo "<p>".$name."</p>";
				$data['menu_name_sc']=$name;
				$db_menu->where("menu_id='%d'", $menu_id)->save($data);
				$menu_id++;				
			}
		}
	}

    public function link_tc(){
    	$db_link=M('link');
    	$db_menu = M('menu');
		$dom=new \DOMDocument();
		$file = "http://localhost/Public/xml/weblink_tc.xml";
		$res = $dom->load($file);
		$links=$dom->getElementsByTagName(Menu);

		$link_id = 1;
		$menu_id = 1;
		foreach ($links as $link) {
			$name = $link->getAttribute("Name");
			$url = $link->getAttribute("URL");
			if($url!=""){
				$data['link_name_tc']=$name;
				$data['link_addr_tc']=$url;
				echo $link_id."  ".$name." ".$url."<br/>";
				$db_link->where("link_id='%d'", $link_id)->save($data);
				$link_id++;
			}else{
				echo "<p>".$name."</p>";
				$data['menu_name_tc']=$name;
				$db_menu->where("menu_id='%d'", $menu_id)->save($data);
				$menu_id++;				
			}
		}
	}



	public function menu_con(){
		$db_menu = M('menu');
		$res = $db_menu->select();
		for($i=0; $i<count($res); $i++){
			$find = $db_menu->where("menu_name_en = '%s'", $res[$i]['parent_name'])->find();
			if($find){
				$data['parent_id'] = $find['menu_id'];
				$db_menu->where("menu_id = '%d'", $res[$i]['menu_id'])->save($data);
			}
		}
	}


	public function getVersion(){
		$db_xml = M('xml');
		$res = $db_xml->where("xml_id = '%d'", 1)->find();
		$version = $res['version'];
		echo $version;
	}
}